package org.wang.generalcode.modules.gen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wang.generalcode.modules.gen.config.GenDataSourceConfig;
import org.wang.generalcode.modules.gen.pojo.entity.GenDataSource;
import org.wang.generalcode.modules.gen.pojo.entity.GenTable;
import org.wang.generalcode.modules.gen.service.DataSourceService;
import org.wang.generalcode.modules.gen.util.DbUtils;
import org.wang.generalcode.modules.gen.util.GenUtils;
import org.wang.generalcode.support.mybaties.Condition;
import org.wang.generalcode.support.mybaties.Query;
import org.wang.generalcode.support.result.Result;
import org.wang.generalcode.support.sm4.SM4Utils;
import org.wang.generalcode.support.utils.Func;

import java.util.List;


@RestController
@AllArgsConstructor
@RequestMapping("/datasource")
@Schema(description = "数据源表", name = "数据源表接口")
public class DataSourceController {

    private final DataSourceService datasourceService;

    @ApiOperationSupport(order = 1)
    @Schema(description = "分页查询", example = "传入query")
    @GetMapping("page")
    public Result<IPage<GenDataSource>> page(Query query) {
        IPage<GenDataSource> page = datasourceService.page(Condition.getPage(query));
        return Result.success(page);
    }

    @ApiOperationSupport(order = 2)
    @Schema(description = "列表查询", example = "")
    @GetMapping("list")
    public Result<List<GenDataSource>> list() {
        List<GenDataSource> list = datasourceService.list();
        return Result.success(list);
    }

    @ApiOperationSupport(order = 3)
    @Schema(description = "查询详情", example = "")
    @GetMapping("/detail")
    public Result<GenDataSource> get(@Parameter(name = "主键", required = true) @RequestParam String id) {
        GenDataSource data = datasourceService.getById(id);
        return Result.success(data);
    }

    @ApiOperationSupport(order = 4)
    @Schema(description = "测试连接", example = "")
    @GetMapping("/test")
    public Result<String> test(@Parameter(name = "主键", required = true) @RequestParam String id) {
        try {
            GenDataSource entity = datasourceService.getById(id);
            entity.setPassword(SM4Utils.decryptStr(entity.getPassword()));
            DbUtils.getConnection(new GenDataSourceConfig(entity));
            return Result.success("连接成功");
        } catch (Exception e) {
            return Result.failed("连接失败，请检查配置信息");
        }
    }

    @ApiOperationSupport(order = 5)
    @Schema(description = "保存", example = "")
    @PostMapping("/save")
    public Result<String> save(@RequestBody GenDataSource entity) {
        return Result.judge(datasourceService.save(entity));
    }

    @ApiOperationSupport(order = 6)
    @Schema(description = "修改", example = "")
    @PostMapping("/update")
    public Result<String> update(@RequestBody GenDataSource entity) {
        return Result.judge(datasourceService.updateById(entity));
    }

    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @Schema(description = "删除", example = "传入ids")
    public Result<String> remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
        return Result.judge(datasourceService.removeBatchByIds(Func.toLongList(ids)));
    }

    /**
     * 根据数据源ID，获取全部数据表
     *
     * @param id 数据源ID
     */
    @ApiOperationSupport(order = 8)
    @Schema(description = "根据数据源ID，获取全部数据表", example = "")
    @GetMapping("table/list")
    public Result<List<GenTable>> tableList(@Parameter(name = "主键", required = true) @RequestParam Long id) {
        try {
            // 获取数据源
            GenDataSourceConfig datasource = datasourceService.get(id);
            // 根据数据源，获取全部数据表
            List<GenTable> tableList = GenUtils.getTableList(datasource);

            return Result.success(tableList);
        } catch (Exception e) {
            return Result.failed("数据源配置错误，请检查数据源配置！");
        }
    }
}
